using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Medianamik.SubsonicDALProvider.DAL
{
	/// <summary>
	/// Strongly-typed collection for the Node class.
	/// </summary>
	[Serializable]
	public partial class NodeCollection : ActiveList<Node,NodeCollection> 
	{	   
		public NodeCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the medianamik_Node table.
	/// </summary>
	[Serializable]
	public partial class Node : ActiveRecord<Node>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Node()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Node(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

        
		public Node(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Node(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }

		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("medianamik_Node", TableType.Table, DataService.GetInstance("MedianamikProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarNodeId = new TableSchema.TableColumn(schema);
				colvarNodeId.ColumnName = "NodeId";
				colvarNodeId.DataType = DbType.Guid;
				colvarNodeId.MaxLength = 0;
				colvarNodeId.AutoIncrement = false;
				colvarNodeId.IsNullable = false;
				colvarNodeId.IsPrimaryKey = true;
				colvarNodeId.IsForeignKey = false;
				colvarNodeId.IsReadOnly = false;
				colvarNodeId.DefaultSetting = @"";
				colvarNodeId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarNodeId);
				
				TableSchema.TableColumn colvarParentNodeId = new TableSchema.TableColumn(schema);
				colvarParentNodeId.ColumnName = "ParentNodeId";
				colvarParentNodeId.DataType = DbType.Guid;
				colvarParentNodeId.MaxLength = 0;
				colvarParentNodeId.AutoIncrement = false;
				colvarParentNodeId.IsNullable = true;
				colvarParentNodeId.IsPrimaryKey = false;
				colvarParentNodeId.IsForeignKey = true;
				colvarParentNodeId.IsReadOnly = false;
				colvarParentNodeId.DefaultSetting = @"";
				
					colvarParentNodeId.ForeignKeyTableName = "medianamik_Node";
				schema.Columns.Add(colvarParentNodeId);
				
				TableSchema.TableColumn colvarContentNodeId = new TableSchema.TableColumn(schema);
				colvarContentNodeId.ColumnName = "ContentNodeId";
				colvarContentNodeId.DataType = DbType.Guid;
				colvarContentNodeId.MaxLength = 0;
				colvarContentNodeId.AutoIncrement = false;
				colvarContentNodeId.IsNullable = true;
				colvarContentNodeId.IsPrimaryKey = false;
				colvarContentNodeId.IsForeignKey = false;
				colvarContentNodeId.IsReadOnly = false;
				colvarContentNodeId.DefaultSetting = @"";
				colvarContentNodeId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarContentNodeId);
				
				TableSchema.TableColumn colvarTypeId = new TableSchema.TableColumn(schema);
				colvarTypeId.ColumnName = "TypeId";
				colvarTypeId.DataType = DbType.Guid;
				colvarTypeId.MaxLength = 0;
				colvarTypeId.AutoIncrement = false;
				colvarTypeId.IsNullable = false;
				colvarTypeId.IsPrimaryKey = false;
				colvarTypeId.IsForeignKey = false;
				colvarTypeId.IsReadOnly = false;
				colvarTypeId.DefaultSetting = @"";
				colvarTypeId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTypeId);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "CreatedOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = false;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				colvarCreatedOn.DefaultSetting = @"";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				TableSchema.TableColumn colvarCreatedBy = new TableSchema.TableColumn(schema);
				colvarCreatedBy.ColumnName = "CreatedBy";
				colvarCreatedBy.DataType = DbType.String;
				colvarCreatedBy.MaxLength = 50;
				colvarCreatedBy.AutoIncrement = false;
				colvarCreatedBy.IsNullable = true;
				colvarCreatedBy.IsPrimaryKey = false;
				colvarCreatedBy.IsForeignKey = false;
				colvarCreatedBy.IsReadOnly = false;
				colvarCreatedBy.DefaultSetting = @"";
				colvarCreatedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedBy);
				
				TableSchema.TableColumn colvarModifiedOn = new TableSchema.TableColumn(schema);
				colvarModifiedOn.ColumnName = "ModifiedOn";
				colvarModifiedOn.DataType = DbType.DateTime;
				colvarModifiedOn.MaxLength = 0;
				colvarModifiedOn.AutoIncrement = false;
				colvarModifiedOn.IsNullable = true;
				colvarModifiedOn.IsPrimaryKey = false;
				colvarModifiedOn.IsForeignKey = false;
				colvarModifiedOn.IsReadOnly = false;
				colvarModifiedOn.DefaultSetting = @"";
				colvarModifiedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedOn);
				
				TableSchema.TableColumn colvarModifiedBy = new TableSchema.TableColumn(schema);
				colvarModifiedBy.ColumnName = "ModifiedBy";
				colvarModifiedBy.DataType = DbType.String;
				colvarModifiedBy.MaxLength = 50;
				colvarModifiedBy.AutoIncrement = false;
				colvarModifiedBy.IsNullable = true;
				colvarModifiedBy.IsPrimaryKey = false;
				colvarModifiedBy.IsForeignKey = false;
				colvarModifiedBy.IsReadOnly = false;
				colvarModifiedBy.DefaultSetting = @"";
				colvarModifiedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedBy);
				
				TableSchema.TableColumn colvarFlags = new TableSchema.TableColumn(schema);
				colvarFlags.ColumnName = "Flags";
				colvarFlags.DataType = DbType.Int32;
				colvarFlags.MaxLength = 0;
				colvarFlags.AutoIncrement = false;
				colvarFlags.IsNullable = false;
				colvarFlags.IsPrimaryKey = false;
				colvarFlags.IsForeignKey = false;
				colvarFlags.IsReadOnly = false;
				colvarFlags.DefaultSetting = @"";
				colvarFlags.ForeignKeyTableName = "";
				schema.Columns.Add(colvarFlags);
				
				TableSchema.TableColumn colvarIsDeleted = new TableSchema.TableColumn(schema);
				colvarIsDeleted.ColumnName = "IsDeleted";
				colvarIsDeleted.DataType = DbType.Boolean;
				colvarIsDeleted.MaxLength = 0;
				colvarIsDeleted.AutoIncrement = false;
				colvarIsDeleted.IsNullable = false;
				colvarIsDeleted.IsPrimaryKey = false;
				colvarIsDeleted.IsForeignKey = false;
				colvarIsDeleted.IsReadOnly = false;
				colvarIsDeleted.DefaultSetting = @"";
				colvarIsDeleted.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsDeleted);
				
				TableSchema.TableColumn colvarLastRevision = new TableSchema.TableColumn(schema);
				colvarLastRevision.ColumnName = "LastRevision";
				colvarLastRevision.DataType = DbType.Int32;
				colvarLastRevision.MaxLength = 0;
				colvarLastRevision.AutoIncrement = false;
				colvarLastRevision.IsNullable = false;
				colvarLastRevision.IsPrimaryKey = false;
				colvarLastRevision.IsForeignKey = false;
				colvarLastRevision.IsReadOnly = false;
				colvarLastRevision.DefaultSetting = @"";
				colvarLastRevision.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLastRevision);
				
				TableSchema.TableColumn colvarPublishedRevision = new TableSchema.TableColumn(schema);
				colvarPublishedRevision.ColumnName = "PublishedRevision";
				colvarPublishedRevision.DataType = DbType.Int32;
				colvarPublishedRevision.MaxLength = 0;
				colvarPublishedRevision.AutoIncrement = false;
				colvarPublishedRevision.IsNullable = false;
				colvarPublishedRevision.IsPrimaryKey = false;
				colvarPublishedRevision.IsForeignKey = false;
				colvarPublishedRevision.IsReadOnly = false;
				colvarPublishedRevision.DefaultSetting = @"";
				colvarPublishedRevision.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPublishedRevision);
				
				TableSchema.TableColumn colvarPath = new TableSchema.TableColumn(schema);
				colvarPath.ColumnName = "Path";
				colvarPath.DataType = DbType.String;
				colvarPath.MaxLength = 500;
				colvarPath.AutoIncrement = false;
				colvarPath.IsNullable = false;
				colvarPath.IsPrimaryKey = false;
				colvarPath.IsForeignKey = false;
				colvarPath.IsReadOnly = false;
				colvarPath.DefaultSetting = @"";
				colvarPath.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPath);
				
				TableSchema.TableColumn colvarDepth = new TableSchema.TableColumn(schema);
				colvarDepth.ColumnName = "Depth";
				colvarDepth.DataType = DbType.Int16;
				colvarDepth.MaxLength = 0;
				colvarDepth.AutoIncrement = false;
				colvarDepth.IsNullable = false;
				colvarDepth.IsPrimaryKey = false;
				colvarDepth.IsForeignKey = false;
				colvarDepth.IsReadOnly = false;
				colvarDepth.DefaultSetting = @"";
				colvarDepth.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDepth);
				
				TableSchema.TableColumn colvarPosition = new TableSchema.TableColumn(schema);
				colvarPosition.ColumnName = "Position";
				colvarPosition.DataType = DbType.Int16;
				colvarPosition.MaxLength = 0;
				colvarPosition.AutoIncrement = false;
				colvarPosition.IsNullable = false;
				colvarPosition.IsPrimaryKey = false;
				colvarPosition.IsForeignKey = false;
				colvarPosition.IsReadOnly = false;
				colvarPosition.DefaultSetting = @"";
				colvarPosition.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPosition);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MedianamikProvider"].AddSchema("medianamik_Node",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("NodeId")]
		public Guid NodeId 
		{
			get { return GetColumnValue<Guid>(Columns.NodeId); }

			set { SetColumnValue(Columns.NodeId, value); }

		}

		  
		[XmlAttribute("ParentNodeId")]
		public Guid? ParentNodeId 
		{
			get { return GetColumnValue<Guid?>(Columns.ParentNodeId); }

			set { SetColumnValue(Columns.ParentNodeId, value); }

		}

		  
		[XmlAttribute("ContentNodeId")]
		public Guid? ContentNodeId 
		{
			get { return GetColumnValue<Guid?>(Columns.ContentNodeId); }

			set { SetColumnValue(Columns.ContentNodeId, value); }

		}

		  
		[XmlAttribute("TypeId")]
		public Guid TypeId 
		{
			get { return GetColumnValue<Guid>(Columns.TypeId); }

			set { SetColumnValue(Columns.TypeId, value); }

		}

		  
		[XmlAttribute("CreatedOn")]
		public DateTime CreatedOn 
		{
			get { return GetColumnValue<DateTime>(Columns.CreatedOn); }

			set { SetColumnValue(Columns.CreatedOn, value); }

		}

		  
		[XmlAttribute("CreatedBy")]
		public string CreatedBy 
		{
			get { return GetColumnValue<string>(Columns.CreatedBy); }

			set { SetColumnValue(Columns.CreatedBy, value); }

		}

		  
		[XmlAttribute("ModifiedOn")]
		public DateTime? ModifiedOn 
		{
			get { return GetColumnValue<DateTime?>(Columns.ModifiedOn); }

			set { SetColumnValue(Columns.ModifiedOn, value); }

		}

		  
		[XmlAttribute("ModifiedBy")]
		public string ModifiedBy 
		{
			get { return GetColumnValue<string>(Columns.ModifiedBy); }

			set { SetColumnValue(Columns.ModifiedBy, value); }

		}

		  
		[XmlAttribute("Flags")]
		public int Flags 
		{
			get { return GetColumnValue<int>(Columns.Flags); }

			set { SetColumnValue(Columns.Flags, value); }

		}

		  
		[XmlAttribute("IsDeleted")]
		public bool IsDeleted 
		{
			get { return GetColumnValue<bool>(Columns.IsDeleted); }

			set { SetColumnValue(Columns.IsDeleted, value); }

		}

		  
		[XmlAttribute("LastRevision")]
		public int LastRevision 
		{
			get { return GetColumnValue<int>(Columns.LastRevision); }

			set { SetColumnValue(Columns.LastRevision, value); }

		}

		  
		[XmlAttribute("PublishedRevision")]
		public int PublishedRevision 
		{
			get { return GetColumnValue<int>(Columns.PublishedRevision); }

			set { SetColumnValue(Columns.PublishedRevision, value); }

		}

		  
		[XmlAttribute("Path")]
		public string Path 
		{
			get { return GetColumnValue<string>(Columns.Path); }

			set { SetColumnValue(Columns.Path, value); }

		}

		  
		[XmlAttribute("Depth")]
		public short Depth 
		{
			get { return GetColumnValue<short>(Columns.Depth); }

			set { SetColumnValue(Columns.Depth, value); }

		}

		  
		[XmlAttribute("Position")]
		public short Position 
		{
			get { return GetColumnValue<short>(Columns.Position); }

			set { SetColumnValue(Columns.Position, value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		private Medianamik.SubsonicDALProvider.DAL.NodeCollection colChildNodeRecords;
		public Medianamik.SubsonicDALProvider.DAL.NodeCollection ChildNodeRecords()
		{
			if(colChildNodeRecords == null)
				colChildNodeRecords = new Medianamik.SubsonicDALProvider.DAL.NodeCollection().Where(Node.Columns.ParentNodeId, NodeId).Load();
			return colChildNodeRecords;
		}

		private Medianamik.SubsonicDALProvider.DAL.NodePropertyValueCollection colNodePropertyValueRecords;
		public Medianamik.SubsonicDALProvider.DAL.NodePropertyValueCollection NodePropertyValueRecords()
		{
			if(colNodePropertyValueRecords == null)
				colNodePropertyValueRecords = new Medianamik.SubsonicDALProvider.DAL.NodePropertyValueCollection().Where(NodePropertyValue.Columns.NodeId, NodeId).Load();
			return colNodePropertyValueRecords;
		}

		private Medianamik.SubsonicDALProvider.DAL.PublicationCollection colPublicationRecords;
		public Medianamik.SubsonicDALProvider.DAL.PublicationCollection PublicationRecords()
		{
			if(colPublicationRecords == null)
				colPublicationRecords = new Medianamik.SubsonicDALProvider.DAL.PublicationCollection().Where(Publication.Columns.NodeId, NodeId).Load();
			return colPublicationRecords;
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Node ActiveRecord object related to this Node
		/// 
		/// </summary>
		public Medianamik.SubsonicDALProvider.DAL.Node ParentNode
		{
			get { return Medianamik.SubsonicDALProvider.DAL.Node.FetchByID(this.ParentNodeId); }

			set { SetColumnValue("ParentNodeId", value.NodeId); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varNodeId,Guid? varParentNodeId,Guid? varContentNodeId,Guid varTypeId,DateTime varCreatedOn,string varCreatedBy,DateTime? varModifiedOn,string varModifiedBy,int varFlags,bool varIsDeleted,int varLastRevision,int varPublishedRevision,string varPath,short varDepth,short varPosition)
		{
			Node item = new Node();
			
			item.NodeId = varNodeId;
			
			item.ParentNodeId = varParentNodeId;
			
			item.ContentNodeId = varContentNodeId;
			
			item.TypeId = varTypeId;
			
			item.CreatedOn = varCreatedOn;
			
			item.CreatedBy = varCreatedBy;
			
			item.ModifiedOn = varModifiedOn;
			
			item.ModifiedBy = varModifiedBy;
			
			item.Flags = varFlags;
			
			item.IsDeleted = varIsDeleted;
			
			item.LastRevision = varLastRevision;
			
			item.PublishedRevision = varPublishedRevision;
			
			item.Path = varPath;
			
			item.Depth = varDepth;
			
			item.Position = varPosition;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varNodeId,Guid? varParentNodeId,Guid? varContentNodeId,Guid varTypeId,DateTime varCreatedOn,string varCreatedBy,DateTime? varModifiedOn,string varModifiedBy,int varFlags,bool varIsDeleted,int varLastRevision,int varPublishedRevision,string varPath,short varDepth,short varPosition)
		{
			Node item = new Node();
			
				item.NodeId = varNodeId;
			
				item.ParentNodeId = varParentNodeId;
			
				item.ContentNodeId = varContentNodeId;
			
				item.TypeId = varTypeId;
			
				item.CreatedOn = varCreatedOn;
			
				item.CreatedBy = varCreatedBy;
			
				item.ModifiedOn = varModifiedOn;
			
				item.ModifiedBy = varModifiedBy;
			
				item.Flags = varFlags;
			
				item.IsDeleted = varIsDeleted;
			
				item.LastRevision = varLastRevision;
			
				item.PublishedRevision = varPublishedRevision;
			
				item.Path = varPath;
			
				item.Depth = varDepth;
			
				item.Position = varPosition;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn NodeIdColumn
        {
            get { return Schema.Columns[0]; }

        }

        
        
        
        public static TableSchema.TableColumn ParentNodeIdColumn
        {
            get { return Schema.Columns[1]; }

        }

        
        
        
        public static TableSchema.TableColumn ContentNodeIdColumn
        {
            get { return Schema.Columns[2]; }

        }

        
        
        
        public static TableSchema.TableColumn TypeIdColumn
        {
            get { return Schema.Columns[3]; }

        }

        
        
        
        public static TableSchema.TableColumn CreatedOnColumn
        {
            get { return Schema.Columns[4]; }

        }

        
        
        
        public static TableSchema.TableColumn CreatedByColumn
        {
            get { return Schema.Columns[5]; }

        }

        
        
        
        public static TableSchema.TableColumn ModifiedOnColumn
        {
            get { return Schema.Columns[6]; }

        }

        
        
        
        public static TableSchema.TableColumn ModifiedByColumn
        {
            get { return Schema.Columns[7]; }

        }

        
        
        
        public static TableSchema.TableColumn FlagsColumn
        {
            get { return Schema.Columns[8]; }

        }

        
        
        
        public static TableSchema.TableColumn IsDeletedColumn
        {
            get { return Schema.Columns[9]; }

        }

        
        
        
        public static TableSchema.TableColumn LastRevisionColumn
        {
            get { return Schema.Columns[10]; }

        }

        
        
        
        public static TableSchema.TableColumn PublishedRevisionColumn
        {
            get { return Schema.Columns[11]; }

        }

        
        
        
        public static TableSchema.TableColumn PathColumn
        {
            get { return Schema.Columns[12]; }

        }

        
        
        
        public static TableSchema.TableColumn DepthColumn
        {
            get { return Schema.Columns[13]; }

        }

        
        
        
        public static TableSchema.TableColumn PositionColumn
        {
            get { return Schema.Columns[14]; }

        }

        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string NodeId = @"NodeId";
			 public static string ParentNodeId = @"ParentNodeId";
			 public static string ContentNodeId = @"ContentNodeId";
			 public static string TypeId = @"TypeId";
			 public static string CreatedOn = @"CreatedOn";
			 public static string CreatedBy = @"CreatedBy";
			 public static string ModifiedOn = @"ModifiedOn";
			 public static string ModifiedBy = @"ModifiedBy";
			 public static string Flags = @"Flags";
			 public static string IsDeleted = @"IsDeleted";
			 public static string LastRevision = @"LastRevision";
			 public static string PublishedRevision = @"PublishedRevision";
			 public static string Path = @"Path";
			 public static string Depth = @"Depth";
			 public static string Position = @"Position";
						
		}

		#endregion
		
		
		
	}

}

